<template>
  <el-drawer
    ref="drawer"
    :title="form.id?'编辑活动': '新增活动'"
    :before-close="handleClose"
    :visible.sync="drawer"
    direction="rtl"
    custom-class="demo-drawer"
    size="50%"
    :wrapper-closable="false"
  >
    <hr>
    <div class="demo-drawer__content" style="padding: 0 20px">
      <el-form ref="form" label-position="top" :model="form" :rules="rules">
        <el-row type="flex">
          <el-form-item label="所属小区" prop="community_code">
            <el-select
              v-model="form.community_code"
              placeholder="请输入"
              style="width: 350px"
            >
              <el-option
                v-for="item in selectHome"
                :key="item.id"
                :label="item.name"
                :value="item.code"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            label="活动主题"
            style="width: 350px; margin-left: 30px"
            prop="title"
          >
            <el-input v-model="form.title" autocomplete="off" />
          </el-form-item>
        </el-row>

        <el-row type="flex">
          <el-form-item label="活动地点" style="width: 350px" prop="place">
            <el-input v-model="form.place" autocomplete="off" />
          </el-form-item>
          <el-form-item
            label="举办单位"
            style="width: 350px; margin-left: 30px"
            prop="sponsor_unit"
          >
            <el-input v-model="form.sponsor_unit" autocomplete="off" />
          </el-form-item>
        </el-row>

        <el-row type="flex">
          <el-form-item label="活动开始时间" prop="begin_time" style="width: 350px">
            <el-date-picker
              v-model="form.begin_time"
              :picker-options="2023-3-22"
              type="datetime"
              placeholder="选择日期"
              style="width: 350px"
            />
          </el-form-item>
          <el-form-item
            prop="end_time"
            label="活动结束时间"
            style="width: 350px; margin-left: 30px"
          >
            <el-date-picker
              v-model="form.end_time"
              :picker-options="2023-3-22"
              type="datetime"
              placeholder="选择日期"
              style="width: 350px"
            />
          </el-form-item>
        </el-row>

        <el-row type="flex">
          <el-form-item label="活动状态" style="width: 350px">
            <el-col>
              <el-radio v-model="form.status" :label="1">有效</el-radio>
              <el-radio v-model="form.status" :label="0">无效</el-radio>
            </el-col>
          </el-form-item>
          <el-form-item label="图片" style="width: 350px; margin-left: 30px">
            <ImageUpload ref="imgUrl" />
          <!-- 上传组件 -->
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="活动内容" />
          <!-- 富文本框 -->
          <quillEditor v-model="form.content" class="quillEditor" />
        </el-row>
      </el-form>
    </div>
    <hr>
    <div class="demo-drawer__footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确定</el-button>
    </div>
  </el-drawer>
</template>
<script>
import { addDataApi, getDetailDataApi, editDataApi } from '@/api/service'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
export default {
  components: {
    quillEditor
  },
  props: {
    drawer: {
      type: Boolean,
      default: false
    },
    selectHome: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      form: {
        begin_time: null,
        community_code: '',
        content: '',
        end_time: null,
        photo: '',
        place: '',
        sponsor_unit: '',
        status: 1,
        title: ''
      },
      rules: {
        community_code: [
          { required: true, message: '请选择小区', trigger: 'blur' }
        ],
        title: [
          { required: true, message: '请填写活动主题', trigger: 'blur' }
        ],
        place: [
          { required: true, message: '请填写活动地点', trigger: 'blur' }
        ],
        sponsor_unit: [
          { required: true, message: '请填写举办单位', trigger: 'blur' }
        ],
        begin_time: [
          { required: true, message: '请选择开始时间', trigger: 'blur' }
        ],
        end_time: [
          { required: true, message: '请选择结束时间', trigger: 'blur' }
        ]
      },
      imgList: []
    }
  },

  methods: {
    handleClose() {
      this.$emit('update:drawer', false)
      this.form = {
        begin_time: null,
        community_code: '',
        content: '',
        end_time: null,
        photo: '',
        place: '',
        sponsor_unit: '',
        status: 1,
        title: ''
      }
    },

    onSubmit() {
      this.$refs.form.validate(async flag => {
        if (!flag) return
        this.form.photo = this.$refs.imgUrl.fileList[0].realtiveUrl
        this.form.begin_time = this.formatTimeThat(this.form.begin_time)
        this.form.end_time = this.formatTimeThat(this.form.end_time)
        this.form.id
          ? await editDataApi(this.form)
          : await addDataApi(this.form)
        // console.log(this.form)
        this.$parent.initData()
        this.$message.success(this.form.id ? '编辑成功' : '添加成功')
        this.handleClose()
      })
    },

    // 转换时间
    formatTimeThat(time) {
      return +new Date(time)
    },
    // 回显
    async editheDate(id) {
      const { data: res } = await getDetailDataApi(id)
      this.$refs.imgUrl.fileList = [
        {
          url: 'http://124.223.14.236:8881' + res.photo, // 显示图片
          realtiveUrl: res.photo // 提交时图片地址
        }
      ]
      this.form = res
    }
  }
}
</script>
<style lang="scss" scoped>
.quillEditor{
  line-height: normal;
}
::v-deep .el-drawer__header {
  margin-bottom: 15px;
  position: relative;
  margin-left: 56px;
}
::v-deep .el-icon-close {
  position: absolute;
  left: -35px;
  top: 18px;
}
::v-deep .el-radio {
  margin-right: 15px;
}
::v-deep .el-form {
  height: 550px;
  overflow: scroll;
  overflow-x:hidden
}
::v-deep .ql-container{
  height: 100px;
}
.demo-drawer__footer{
  float: right;
  margin-right: 30px;
}
</style>
